package com.zking.seckillpro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zking.seckillpro.model.Goods;
import com.zking.seckillpro.mapper.GoodsMapper;
import com.zking.seckillpro.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zking.seckillpro.util.JsonResponseBody;
import com.zking.seckillpro.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * ��Ʒ��Ϣ�� 服务实现类
 * </p>
 *
 * @author zking
 * @since 2022-03-06
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public JsonResponseBody<List<Goods>> queryGoodsPager(Goods goods, PageBean pageBean) {
    //定义条件构造器
        QueryWrapper<Goods> query=new QueryWrapper<>();
        //判断商品名称是否为空
        if(StringUtils.isNotBlank(goods.getGoodsName()))
            query.like("goods_name",goods.getGoodsName());
        //按照商品编号降序排序
        query.orderByDesc("gid");
        //设置分页条件并实现查询
        Page<Goods> goodsPage = goodsMapper.selectPage(new Page<Goods>()
                        .setSize(pageBean.getRows())//显示条数
                        .setCurrent(pageBean.getPage()),//当前页码
                query);

        return new JsonResponseBody<>(goodsPage.getRecords(),goodsPage.getTotal());
    }
}
